#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 05-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;

using Thn;
#endregion

namespace Thn.Interface.Vcl
{
    /// <summary>
    /// A simple font style with simple shadow by rendering two different color texts.
    /// </summary>
#if DEBUG
    public
#else
    public
#endif
 class SimpleShadowedFontStyle : IFontStyle
    {
        #region Font
        private Font mFont;
        /// <summary>
        /// Gets/Sets the font used for rendering text.
        /// </summary>
        public Font Font
        {
            get { return mFont; }
            set { mFont = value; }
        }
        #endregion

        #region Color
        private Color mColor;
        /// <summary>
        /// Gets/Sets color of the font.
        /// </summary>
        public Color Color
        {
            get { return mColor; }
            set { mColor = value; }
        }
        #endregion

        #region Shadow Color
        private Color mShadowColor = Color.DarkGray;
        /// <summary>
        /// Gets/Sets color of the shadow.
        /// </summary>
        public Color ShadowColor
        {
            get { return mShadowColor; }
            set { mShadowColor = value; }
        }
        #endregion

        #region Offset X
        private int mOffsetX = 2;
        /// <summary>
        /// Gets/Sets x-axis offset from the main text for drawing shadow text.
        /// </summary>
        public int OffsetX
        {
            get { return mOffsetX; }
            set { mOffsetX = value; }
        }
        #endregion

        #region Offset Y
        private int mOffsetY = 2;
        /// <summary>
        /// Gets/Sets y-axis offset from the main text for drawing shadow text.
        /// </summary>
        public int OffsetY
        {
            get { return mOffsetY; }
            set { mOffsetY = value; }
        }
        #endregion

        #region Draw String
        /// <summary>
        /// Render text using this style.
        /// </summary>
        /// <param name="text">The message to be rendered.</param>
        /// <param name="canvas">The <see cref="Graphics"/> to render to.</param>
        /// <param name="r">The boundary where text will be rendered within.</param>
        /// <param name="alignment">Alignment settings.</param>
        /// <param name="wordWrap">Whether long text shall be wrapped to multiple lines.</param>
        public void DrawString(string text, Graphics canvas, Rectangle r, ContentAlignment alignment, bool wordWrap)
        {
            Brush bNormal = new SolidBrush(mColor);
            Brush bShadow = new SolidBrush(mShadowColor);

            #region Apply format settings
            StringFormat format = new StringFormat();
            format.Trimming = StringTrimming.EllipsisCharacter;
            if (!wordWrap) format.FormatFlags = StringFormatFlags.NoWrap;
            #region Defensive tracing
            else
            {
                //default settings enables word wrap                
            }
            #endregion

            switch (alignment)
            {
                case ContentAlignment.BottomCenter:
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Far;
                    break;
                case ContentAlignment.BottomLeft:
                    format.Alignment = StringAlignment.Near;
                    format.LineAlignment = StringAlignment.Far;
                    break;
                case ContentAlignment.BottomRight:
                    format.Alignment = StringAlignment.Far;
                    format.LineAlignment = StringAlignment.Far;
                    break;
                case ContentAlignment.MiddleCenter:
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Center;
                    break;
                case ContentAlignment.MiddleLeft:
                    format.Alignment = StringAlignment.Near;
                    format.LineAlignment = StringAlignment.Center;
                    break;
                case ContentAlignment.MiddleRight:
                    format.Alignment = StringAlignment.Far;
                    format.LineAlignment = StringAlignment.Center;
                    break;
                case ContentAlignment.TopCenter:
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Near;
                    break;
                case ContentAlignment.TopLeft:
                    format.Alignment = StringAlignment.Near;
                    format.LineAlignment = StringAlignment.Near;
                    break;
                case ContentAlignment.TopRight:
                    format.Alignment = StringAlignment.Far;
                    format.LineAlignment = StringAlignment.Near;
                    break;
                default:
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Center;
                    break;
            }
            #endregion

            //render shadow text
            Rectangle rShadow = new Rectangle(r.Left + mOffsetX, r.Top + mOffsetY, r.Width, r.Height);
            canvas.DrawString(text, mFont, bShadow, rShadow, format);

            //render normal text
            canvas.DrawString(text, mFont, bNormal, r, format);

            //clean up
            bNormal.Dispose();
            bShadow.Dispose();
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Create a new instance.
        /// </summary>
        public SimpleShadowedFontStyle(Font font, Color color)
        {
            mFont = font;
            mColor = color;
        }

        /// <summary>
        /// Create a new instance.
        /// </summary>
        public SimpleShadowedFontStyle(Font font, Color color, Color shadowColor, int offsetX, int offsetY)
        {
            mFont = font;
            mColor = color;
            mShadowColor = shadowColor;
            mOffsetX = offsetX;
            mOffsetY = offsetY;
        }
        #endregion
    }
}
